package com.ruoyi.web.controller.proimpl;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ExaLeave;
import com.ruoyi.domain.ProImpl;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IProImplService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 【施工材料审批】Controller
 *
 * @author ruoyi
 * @date 2020-07-08
 */
@Controller
@RequestMapping("/ruoyi/implVerify")
public class ProImplVerifyContriller extends BaseController {

    private String prefix = "/ruoyi/impl";

    @Autowired
    private IProImplService proImplService;

    @RequiresPermissions("ruoyi:implVerify:view")
    @GetMapping()
    public String impl() {
        return prefix + "/implVerify";
    }


    /**
     * 查询总项目对应的施工资料列表
     */
    @RequiresPermissions("ruoyi:completed:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo list(Long sid) {
        startPage();
        List<ProImpl> list = proImplService.selectProImplLists(sid);
        list.removeIf(p ->p.getImState()==0);
        return getDataTable(list);
    }

    /**
     * 首页查询对应的施工资料
     * @param idss
     * @return
     */
    @RequiresPermissions("ruoyi:implVerify:listss")
    @PostMapping("/listss")
    @ResponseBody
    public TableDataInfo listss(Long idss) {
        startPage();
        List<ProImpl> list = proImplService.selectProImplLists(idss);
        return getDataTable(list);
    }

    /**
     * 查询施工资料列表
     */
    @RequiresPermissions("ruoyi:implVerify:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProImpl proImpl) {
        startPage();
        List<ProImpl> list = proImplService.selectProImplList(proImpl);
        return getDataTable(list);
    }


    /**
     * 导出施工资料列表
     */
    @RequiresPermissions("ruoyi:implVerify:export")
    @Log(title = "施工材料审批", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProImpl proImpl) {
        List<ProImpl> list = proImplService.selectProImplList(proImpl);
        ExcelUtil<ProImpl> util = new ExcelUtil<ProImpl>(ProImpl.class);
        return util.exportExcel(list, "施工材料审批");
    }

    /**
     * 新增施工资料
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map) {
        map.put("pId", pId);
        return prefix + "/add";
    }

    /**
     * 新增保存施工资料
     */
    @RequiresPermissions("ruoyi:implVerify:add")
    @Log(title = "施工资料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, ProImpl proImpl) throws IOException {
        //上传文件路径
        String filePach = Global.getUploadPath();
        //上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePach, file);
        proImpl.setImData(fileName);
        String name = ShiroUtils.getLoginName();
        proImpl.setImTheHeir(name);
        return toAjax(proImplService.insertProImpl(proImpl));
    }

    /**
     * 删除施工资料
     */
    @RequiresPermissions("ruoyi:implVerify:remove")
    @Log(title = "施工资料", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(proImplService.deleteProImplByIds(ids));
    }

    /**
     * 通过项目外键查询Get
     */
    @GetMapping("/implss/{bId}")
    public String filespByFd(@PathVariable("bId") Long bId, ModelMap mmap) {
        /*List<FileUploading> filesps = fileUploadingService.selectFileUploadingByFdlist(bId);
        mmap.put("filesps", filesps);*/
        mmap.put("bId", bId);
        return prefix + "/implss";
    }

    /**
     *
     * 下载功能
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("pUploading") String fAccessory, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(fAccessory, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }
    /**
     * 驳回【请假】
     */
    @GetMapping("/edit/{imId}")
    public String edit(@PathVariable("imId") Long imId, ModelMap mmap)
    {
        proImplService.editExaStatus(imId);
        ProImpl proImpl = proImplService.selectProImplById(imId);
        mmap.put("proImpl", proImpl);
        return prefix + "/Verifyedit";
    }

    /**
     * 驳回保存【审批】
     */
    @RequiresPermissions("ruoyi:implVerify:edit")
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult confirm(ProImpl proImpl) {
        return toAjax(proImplService.updateProImpl(proImpl));
    }

    /**
     * 通过申请
     */
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/consent")
    @ResponseBody
    public AjaxResult consent(Long imId) {
        int leave = proImplService.conExaStatus(imId);


        return success();
    }
}